Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tiny-typed-emitter

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-typed-emitter

Fully type-checked EventEmitter

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
274K
decreased by-7.37%
Maintainers
1
Weekly downloads
 
Created

What is tiny-typed-emitter?

The tiny-typed-emitter package is a lightweight, strongly-typed event emitter for TypeScript and JavaScript. It allows you to create and manage events with type safety, ensuring that event listeners and emitters are correctly typed.

What are tiny-typed-emitter's main functionalities?

Basic Event Emission

This feature allows you to define and emit events with specific types. The code sample demonstrates how to create a custom event emitter, add listeners for 'data' and 'error' events, and emit those events with the appropriate data types.

const { TypedEmitter } = require('tiny-typed-emitter');

class MyEmitter extends TypedEmitter {
  // Define event types
  on(event: 'data', listener: (data: string) => void): this;
  on(event: 'error', listener: (error: Error) => void): this;
}

const emitter = new MyEmitter();

// Add listeners
emitter.on('data', (data) => {
  console.log('Data received:', data);
});

emitter.on('error', (error) => {
  console.error('Error occurred:', error);
});

// Emit events
emitter.emit('data', 'Hello, World!');
emitter.emit('error', new Error('Something went wrong!'));

Removing Event Listeners

This feature allows you to remove event listeners. The code sample demonstrates how to add a listener for the 'data' event, emit the event, remove the listener, and then emit the event again to show that the listener has been removed.

const { TypedEmitter } = require('tiny-typed-emitter');

class MyEmitter extends TypedEmitter {
  on(event: 'data', listener: (data: string) => void): this;
}

const emitter = new MyEmitter();

function onData(data) {
  console.log('Data received:', data);
}

// Add listener
emitter.on('data', onData);

// Emit event
emitter.emit('data', 'Hello, World!');

// Remove listener
emitter.off('data', onData);

// Emit event again (no output expected)
emitter.emit('data', 'Hello again!');

Once Event Listeners

This feature allows you to add a listener that will be called only once. The code sample demonstrates how to add a 'once' listener for the 'data' event, emit the event to trigger the listener, and then emit the event again to show that the listener is not called a second time.

const { TypedEmitter } = require('tiny-typed-emitter');

class MyEmitter extends TypedEmitter {
  on(event: 'data', listener: (data: string) => void): this;
  once(event: 'data', listener: (data: string) => void): this;
}

const emitter = new MyEmitter();

// Add a once listener
emitter.once('data', (data) => {
  console.log('Data received:', data);
});

// Emit event (listener will be called)
emitter.emit('data', 'Hello, World!');

// Emit event again (listener will not be called)
emitter.emit('data', 'Hello again!');

Other packages similar to tiny-typed-emitter

Keywords

FAQs

Package last updated on 24 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc